package cn.gcjxsc.payment.controller;

import cn.gcjxsc.common.data.wx.redpack.QueryRedPackResponse;
import cn.gcjxsc.payment.data.WeChatConfig;
import cn.gcjxsc.payment.service.wx.WeChatApiService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 微信红包控制器
 *
 * @author pumbaa
 * @since 1.0
 */
@RestController
@RequestMapping("/api/wx/redpack")
public class RedPackController {
    @Autowired
    private ResourceLoader resourceLoader;
    @Autowired
    private WeChatConfig config;


    /**
     * 查询红包发送记录
     *
     * @param tradeNo 商户红包交易号
     * @return 返回查询结果
     * @see QueryRedPackResponse
     */
    @RequestMapping("/query")
    public QueryRedPackResponse query(@RequestParam("tradeNo") String tradeNo) throws Exception {
        Resource resource = resourceLoader.getResource("classpath:/certs/apiclient_cert.p12");
        String path = resource.getFile().getAbsolutePath();

        WeChatApiService service = new WeChatApiService();
        return service.queryRedPack(config, tradeNo, path);
    }
}
